package cn.yerly.sakura.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.yerly.sakura.domain.ItemAttribute;
import cn.yerly.sakura.repository.ItemAttributeRepository;
import cn.yerly.sakura.repository.search.ItemAttributeSearchRepository;
import cn.yerly.sakura.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing ItemAttribute.
 */
@RestController
@RequestMapping("/api")
public class ItemAttributeResource {

    private final Logger log = LoggerFactory.getLogger(ItemAttributeResource.class);

    @Inject
    private ItemAttributeRepository itemAttributeRepository;

    @Inject
    private ItemAttributeSearchRepository itemAttributeSearchRepository;

    /**
     * POST  /itemAttributes -> Create a new itemAttribute.
     */
    @RequestMapping(value = "/itemAttributes",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<Void> create(@RequestBody ItemAttribute itemAttribute) throws URISyntaxException {
        log.debug("REST request to save ItemAttribute : {}", itemAttribute);
        if (itemAttribute.getId() != null) {
            return ResponseEntity.badRequest().header("Failure", "A new itemAttribute cannot already have an ID").build();
        }
        itemAttributeRepository.save(itemAttribute);
        itemAttributeSearchRepository.save(itemAttribute);
        return ResponseEntity.created(new URI("/api/itemAttributes/" + itemAttribute.getId())).build();
    }

    /**
     * PUT  /itemAttributes -> Updates an existing itemAttribute.
     */
    @RequestMapping(value = "/itemAttributes",
        method = RequestMethod.PUT,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<Void> update(@RequestBody ItemAttribute itemAttribute) throws URISyntaxException {
        log.debug("REST request to update ItemAttribute : {}", itemAttribute);
        if (itemAttribute.getId() == null) {
            return create(itemAttribute);
        }
        itemAttributeRepository.save(itemAttribute);
        itemAttributeSearchRepository.save(itemAttribute);
        return ResponseEntity.ok().build();
    }

    /**
     * GET  /itemAttributes -> get all the itemAttributes.
     */
    @RequestMapping(value = "/itemAttributes",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<List<ItemAttribute>> getAll(@RequestParam(value = "page" , required = false) Integer offset,
                                  @RequestParam(value = "per_page", required = false) Integer limit)
        throws URISyntaxException {
        Page<ItemAttribute> page = itemAttributeRepository.findAll(PaginationUtil.generatePageRequest(offset, limit));
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/itemAttributes", offset, limit);
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /itemAttributes/:id -> get the "id" itemAttribute.
     */
    @RequestMapping(value = "/itemAttributes/{id}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<ItemAttribute> get(@PathVariable Long id) {
        log.debug("REST request to get ItemAttribute : {}", id);
        return Optional.ofNullable(itemAttributeRepository.findOne(id))
            .map(itemAttribute -> new ResponseEntity<>(
                itemAttribute,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /itemAttributes/:id -> delete the "id" itemAttribute.
     */
    @RequestMapping(value = "/itemAttributes/{id}",
            method = RequestMethod.DELETE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public void delete(@PathVariable Long id) {
        log.debug("REST request to delete ItemAttribute : {}", id);
        itemAttributeRepository.delete(id);
        itemAttributeSearchRepository.delete(id);
    }

    /**
     * SEARCH  /_search/itemAttributes/:query -> search for the itemAttribute corresponding
     * to the query.
     */
    @RequestMapping(value = "/_search/itemAttributes/{query}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<ItemAttribute> search(@PathVariable String query) {
        return StreamSupport
            .stream(itemAttributeSearchRepository.search(queryString(query)).spliterator(), false)
            .collect(Collectors.toList());
    }
}
